package tk.chenyilin.server;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.DefaultListModel;

/**
 * 最开始实现的命令行服务器端
 * 图形界面客户端是基于命令行客户端的
 * 遗留下来了一些命令行的内容
 * @author 陈奕霖
 *
 */
public class Server {
	static Scanner scanner = new Scanner(System.in);
	Map<String, User> map = new HashMap<String, User>();
	DefaultListModel<String> model=new DefaultListModel<>();
	int port;

	public Server(String port) {
		this.port = Integer.parseInt(port);
	}
	
	public Server(int port){
		this.port=port;
	}

	public static void main(String[] args) {
		System.out.print("Port:");
		String port = scanner.next();
		Server server = new Server(port);
		server.startServer();
	}
	
	public void startServer(){
		try {
			ServerSocket serverSocket = new ServerSocket(port);
			while (true) {
				Socket socket = serverSocket.accept();
				Connection connection=new Connection(this,socket);
				connection.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void print(String message){
		System.out.println(message);
	}
}
